import { request } from '../request';

// ---------- 登录、刷新 token、退出 ----------

/**
 * 登录
 *
 * @param userName 登录名
 * @param password 登录密码
 * @returns Api.Auth.LoginToken
 */
export function fetchLogin(userName: string, password: string, moduleCode: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/login',
    method: 'post',
    data: {
      userName,
      password,
      moduleCode
    }
  });
}

/**
 * 刷新 token
 *
 * @param refreshToken string
 * @returns Api.Auth.LoginToken
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/refresh_token',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * 退出
 *
 * @returns boolean
 */
export function fetchLogout() {
  return request<boolean>({
    url: '/auth/logout',
    method: 'post'
  });
}

/** Get user info */
export function fetchGetUserInfo() {
  return request<Api.Auth.UserInfo>({ url: '/auth/user_info' });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/auth/error', params: { code, msg } });
}
